package com.joker.orange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joker.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.joker.orange.domain.bo.PayConfigBo;
import com.joker.orange.domain.vo.PayConfigVo;
import com.joker.orange.domain.PayConfig;
import com.joker.orange.mapper.PayConfigMapper;
import com.joker.orange.service.IPayConfigService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 支付管理Service业务层处理
 *
 * @author Joker
 * @date 2024-07-31
 */
@RequiredArgsConstructor
@Service
public class PayConfigServiceImpl implements IPayConfigService {

    private final PayConfigMapper baseMapper;

    /**
     * 查询支付管理
     */
    @Override
    public PayConfigVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询支付管理列表
     */
    @Override
    public TableDataInfo<PayConfigVo> queryPageList(PayConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PayConfig> lqw = buildQueryWrapper(bo);
        Page<PayConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询支付管理列表
     */
    @Override
    public List<PayConfigVo> queryList(PayConfigBo bo) {
        LambdaQueryWrapper<PayConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PayConfig> buildQueryWrapper(PayConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PayConfig> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getPayName()), PayConfig::getPayName, bo.getPayName());
        lqw.eq(StringUtils.isNotBlank(bo.getAppId()), PayConfig::getAppId, bo.getAppId());
        lqw.eq(StringUtils.isNotBlank(bo.getSecret()), PayConfig::getSecret, bo.getSecret());
        lqw.eq(StringUtils.isNotBlank(bo.getPayUrl()), PayConfig::getPayUrl, bo.getPayUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getNotifyUrl()), PayConfig::getNotifyUrl, bo.getNotifyUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getPayType()), PayConfig::getPayType, bo.getPayType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), PayConfig::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增支付管理
     */
    @Override
    public Boolean insertByBo(PayConfigBo bo) {
        PayConfig add = BeanUtil.toBean(bo, PayConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改支付管理
     */
    @Override
    public Boolean updateByBo(PayConfigBo bo) {
        PayConfig update = BeanUtil.toBean(bo, PayConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PayConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除支付管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
